package linkedlist.test817;

import listbyorder.utils.ListNode;

import java.util.HashSet;
import java.util.Set;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/15 12:43
 */
public class Solution1 {

    public int numComponents(ListNode head, int[] G) {
        Set<Integer> set = new HashSet<Integer>();
        for (int n : G) {
            set.add(n);
        }
        int ans = 0;
        ListNode t = head;
        while (t != null) {
            if (!set.contains(t.val)) {
                t = t.next;
            } else {
                while (t != null && set.contains(t.val)) {
                    t = t.next;
                }
                ans++;
            }
        }
        return ans;
    }
}
